﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.AuditLogging.LogEntry;
using System.Diagnostics;
using PSE.AuditLogging.Repository;
using PSE.AuditLogging.Common.Exception;
using PSE.AuditLogging.Client.Configuration;
using System.Configuration;
using PSE.Framework.ErrorLogging;

namespace PSE.AuditLogging.Client
{
    public class LogSender
    {
        static object _syncRoot;
        static LogStoreRouter _messageRouter;

        static LogSender()
        {
            _syncRoot = new object();
        }

        /// <summary>
        /// Escreve uma entrada de log
        /// </summary>
        /// <param name="entry"></param>
        public static void Send(BaseLogEntry entry)
        {
            try
            {
                lock (_syncRoot)
                {
                    if (_messageRouter == null)
                        _messageRouter = new LogStoreRouter();
                }

                _messageRouter.SendPackage(LogEntryPacker.Pack(entry));
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Escreve uma entrada de log empacotada, utilizado para reenvio de mensagens armazenadas no arquivo
        /// de contingência
        /// </summary>
        /// <param name="entryPackage"></param>
        internal static void Resend(LogEntryPackage entryPackage)
        {
            lock (_syncRoot)
            {
                if (_messageRouter == null)
                    _messageRouter = new LogStoreRouter();
            }

            _messageRouter.ResendPackage(entryPackage);
        }
    }
}
